#ifndef TOUCH_CALIBRATIONNINE_H
#define TOUCH_CALIBRATIONNINE_H

#include <QDialog>
#include <QWSPointerCalibrationData>
#include <QLabel>
#include <QtGui/QHBoxLayout>
#include <QtGui/QSpacerItem>
#include <QWSServer>
//#include "../modal/modal_base_dialog.h"

#define MCGS_CALIBRATION_DRAWCROSS_TIMEOUT_NINE 20
#define MCGS_CALIBRATION_SCREEN_POINTS_NINE 	   20
#define MCGS_CALIBRATION_PROBE_SET_NINE      12
#define MCGS_TPC_LARGE_DISPLAY_NINE           600

class calibration_notify_event : public QEvent
{
public:
    explicit calibration_notify_event(Type e);

public:
    static const Type _calibration_notify_event;
};

class touch_calibration_nine : public QDialog
{
    Q_OBJECT

public:
    typedef enum screen_matrix_location_e
    {
        SCREEN_CENTRE,
        SCREEN_TOP,
        SCREEN_TOP_LEFT,
        SCREEN_LEFT,
        SCREEN_BOTTOM_LEFT,
        SCREEN_BOTTOM,
        SCREEN_BOTTOM_RIGHT,
        SCREEN_RIGHT,
        SCREEN_TOP_RIGHT,
        SCREEN_LASTLOCATION = SCREEN_TOP_RIGHT,
        SCREEN_MATRIX_X
    }screen_matrix_location_t;

public:
    explicit touch_calibration_nine(QWidget* parent = 0);
    ~touch_calibration_nine();
    int calibration_exec();
    int calibration_exec_without_clear();

protected:
    virtual void paintEvent(QPaintEvent*);
    virtual void mousePressEvent(QMouseEvent*);
    virtual void mouseMoveEvent(QMouseEvent*);
    virtual void mouseReleaseEvent(QMouseEvent*);
    virtual void accept();

private:
    void set_ui(QDialog* dlg);
    void calibration_confirm_ui(QPainter* painter);
    bool linear_coefficient_verify();

    void client_window_show();

private:
    void read_calibration_paras();
    void write_calibration_paras();
    void clear_calibration_paras();

protected slots:
    void probe_calibration();
    void draw_cross();
    void forbid_window_popped();

private:
    QLabel* _step1;
    QLabel* _step2;

    QWSPointerCalibrationData _former_data;
    QWSPointerCalibrationData _data;
    QPoint _other_screen_points[QWSPointerCalibrationData::LastLocation][MCGS_CALIBRATION_SCREEN_POINTS_NINE];

    QPoint _screen_nine_matrix[SCREEN_MATRIX_X];
    QPoint _other_screen_points_for_nine[SCREEN_LASTLOCATION][MCGS_CALIBRATION_SCREEN_POINTS_NINE];

    int _pressCount;
    int _algorithm_points;

    QTimer* _press_timer;
    QTimer* _draw_timer;
    QTimer* _monitor_client;
    int _which;

    QPoint _average_array[MCGS_CALIBRATION_PROBE_SET_NINE];
    int _average_index;
    int _total_num;

    bool _calibration_ret;

private:
    int _a, _b, _c;
    int _d, _e, _f;
    int _s;
};

#endif
